package com.legna.mhxy.support.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.legna.mhxy.support.dao.RingCategoryDao;
import com.legna.mhxy.support.dao.RingTypeDao;
import com.legna.mhxy.support.dao.UserRingDao;
import com.legna.mhxy.support.entity.RingCategory;
import com.legna.mhxy.support.entity.RingType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Slf4j
public class RingTypeService {
    private final RingCategoryDao ringCategoryDao;
    private final RingTypeDao ringTypeDao;
    private final UserRingDao userRingDao;

    public List<RingType> listAll(Long categoryId,Integer categoryType, Integer grade){
        LambdaQueryWrapper<RingType> qw = new LambdaQueryWrapper<>();
        if (categoryType != null){
            List<RingCategory> ringCategories = ringCategoryDao.selectList(new LambdaQueryWrapper<RingCategory>().eq(RingCategory::getType, categoryType));
            List<Long> categoryIds = ringCategories.stream().map(RingCategory::getId).collect(Collectors.toList());
            qw.in(RingType::getCategoryId,categoryIds);
        }
        if (categoryId != null){
            qw.eq(RingType::getCategoryId,categoryId);
        }
        if (grade != null){
            qw.eq(RingType::getGrade,grade);
        }
        qw.orderByAsc(RingType::getSort);
        return ringTypeDao.selectList(qw);
    }
}
